package com.tuling.jucdemo.atomic;

import com.tuling.jucdemo.factory.UnsafeFactory;
import sun.misc.Unsafe;

/**
 * CAS操作测试
 */
public class CASTest {
    public static void main(String[] args) {
        Entity entity = new Entity();
        Unsafe unsafe = UnsafeFactory.getUnsafe();
        //偏移量 对象头8个字节 64位压缩指针4个字节 int类型4个字节  默认是8的整数倍 从12开始占4个字节
        long offset = UnsafeFactory.getFieldOffset(unsafe, Entity.class, "x");
        System.out.println(offset);

        boolean successful;

        // 4个参数分别是：对象实例、字段的内存偏移量、字段期望值、字段更新值
        successful = unsafe.compareAndSwapInt(entity, offset, 0, 3);
        System.out.println(successful+"\t"+entity.x);

        successful = unsafe.compareAndSwapInt(entity,offset,3,5);
        System.out.println(successful+"\t"+entity.x);

        successful = unsafe.compareAndSwapInt(entity,offset,3,8);
        System.out.println(successful+"\t"+entity.x);
    }
}

class Entity {
    int x;
}
